package com.huarui.saleInvoiceItem.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.ListModel;

import com.huarui.initial.util.InitialUtil;
import com.huarui.saleInvoiceItem.service.SaleInvoiceItemService;
import com.huarui.user.model.SessionMessage;
import com.huarui.user.security.UserHolder;
import com.huarui.util.JsonUtil;
import com.opensymphony.xwork2.Action;

public class SaleInvoiceItemAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4130226656983851956L;
	private SaleInvoiceItemService saleInvoiceItemService;
	private Integer saleInvoiceId;
	private Integer customerId;
	private SessionMessage sm;
	private String searchConditions;
	private String ids;
	@SuppressWarnings("unchecked")
	private ListModel<Map> listModel=new ListModel<Map>();
	private Integer limit;
	private Integer start;
	private String sort;
	private String dir;
	
	private Map<String, Object> resultMap=new HashMap<String, Object>();

	@SuppressWarnings("unchecked")
	public String extList() {
		sm = UserHolder.getCurrentSessionMessage();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("companyId", sm.getCompanyId());
		map.put("customerId", customerId);
		map.put("saleInvoiceId", saleInvoiceId);
		
		List<Map> list = saleInvoiceItemService.getListByCondition(map);
		resultMap.put("root", list);
		return Action.SUCCESS;
	}
	@SuppressWarnings("unchecked")
	public String extListPage(){
		sm=UserHolder.getCurrentSessionMessage();
		listModel.setPageSize(limit == null ? 10 : limit);
		listModel.setPage(start == null ? 0 : start / limit + 1);
		listModel.setOrderBy(sort==null?"id":sort);
		if (dir != null){
			listModel.setDesc(dir.equals("DESC") ? true : false);
		}else{
			listModel.setDesc(true);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("companyId", sm.getCompanyId());
		map.put("customerId", customerId);
		map.put("saleInvoiceId", saleInvoiceId);
		if(searchConditions!=null){
			List<String> conditions = new ArrayList<String>();
			List<String> numList = new ArrayList<String>();
			List<String> strList = new ArrayList<String>();
			InitialUtil.addToCollection(conditions, InitialUtil.split(searchConditions, " ,"));
			for(String str:conditions){
				if(InitialUtil.isNum(str)){
					numList.add(str);
				}else{
					strList.add(str);
				}
			}	
			map.put("numList",numList);
			map.put("strList",strList);
		}
		if(ids!=null){
			Integer[] idsArray=JsonUtil.getIntegerArray4Json(ids);
			map.put("ids", idsArray);
		}
		List<Map> list = saleInvoiceItemService.getListByCondition(listModel,map);
		resultMap.put("root", list);
		return Action.SUCCESS;
	}
	public Map<String, Object> getResultMap() {
		return resultMap;
	}

	

	public void setSaleInvoiceItemService(
			SaleInvoiceItemService saleInvoiceItemService) {
		this.saleInvoiceItemService = saleInvoiceItemService;
	}
	public void setSaleInvoiceId(Integer saleInvoiceId) {
		this.saleInvoiceId = saleInvoiceId;
	}
	public void setLimit(Integer limit) {
		this.limit = limit;
	}
	public void setStart(Integer start) {
		this.start = start;
	}
	public void setSort(String sort) {
		this.sort = sort;
	}
	public void setDir(String dir) {
		this.dir = dir;
	}
	public void setPayInvoiceId(Integer payInvoiceId) {
		this.saleInvoiceId = payInvoiceId;
	}

	public void setCustomerId(Integer customerId) {
		this.customerId = customerId;
	}

	public void setSm(SessionMessage sm) {
		this.sm = sm;
	}

	public void setSearchConditions(String searchConditions) {
		this.searchConditions = searchConditions;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}

}
